package com.web.hilover.service.impl;

import com.web.hilover.entity.LikeRecord;
import com.web.hilover.mapper.LikeRecordMapper;
import com.web.hilover.mapper.CoupleMapper;
import com.web.hilover.mapper.CommentMapper;
import com.web.hilover.service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LikeServiceImpl implements LikeService {
    
    @Autowired
    private LikeRecordMapper likeRecordMapper;
    
    @Autowired
    private CoupleMapper coupleMapper;
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Override
    @Transactional
    public void likeCouple(Long coupleId, Long userId) {
        if (hasLiked(coupleId, userId, 1)) {
            throw new RuntimeException("已经点赞过了");
        }
        
        LikeRecord record = LikeRecord.builder()
                .userId(userId)
                .targetId(coupleId)
                .type(1)
                .build();
                
        likeRecordMapper.insert(record);
        coupleMapper.incrementLikes(coupleId);
    }
    
    @Override
    @Transactional
    public void likeComment(Long commentId, Long userId) {
        if (hasLiked(commentId, userId, 2)) {
            throw new RuntimeException("已经点赞过了");
        }
        
        LikeRecord record = LikeRecord.builder()
                .userId(userId)
                .targetId(commentId)
                .type(2)
                .build();
                
        likeRecordMapper.insert(record);
        commentMapper.incrementLikes(commentId);
    }
    
    @Override
    public boolean hasLiked(Long targetId, Long userId, Integer type) {
        return likeRecordMapper.exists(targetId, userId, type);
    }
    
    @Override
    public List<Map<String, Object>> getLikeRecordsByUser(Long userId) {
        return likeRecordMapper.findByUserWithDetail(userId);
    }
} 